<!--
 * @Description: 
 * @Author: charles
 * @Date: 2021-07-12 10:56:47
 * @LastEditors: YuanYu
 * @LastEditTime: 2021-10-11 20:20:20
-->
<template>
  <div>
    <!-- 头部 -->
    <Header></Header>
    <!-- {{category.name}} -->
    <!-- 中部 -->
    <div class="content">
      <div class="wrapper">
        <div class="Title">{{ category.name }}</div>
        <div class="container">
          <div class="arcticleArea" v-if="articleData.total > 0">
            <div
              class="arcticle"
              v-for="item in articleData.list"
              :key="item.id"
            >
              <div class="logo">
                <img :src="item.cover" alt="" />
              </div>
              <div class="right">
                <div class="minTitle" @click="toArticle(item)">
                  {{ item.title }}
                </div>
                <div class="name">作者：{{ item.baseUser.realname }}</div>
                <div class="time">
                  发布时间：{{ item.publishTime | fmtDate }}
                </div>
              </div>
            </div>
            <div class="page">
              <el-pagination
                background
                @current-change="handleCurrentChange"
                :current-page.sync="param.page"
                :page-size="param.pageSize"
                layout="total, prev, pager, next"
                :total="articleData.total"
              >
              </el-pagination>
            </div>
          </div>
          <div class="noData" v-else>暂无数据</div>
          <div class="list" v-if="articleData.total > 0">
            <div class="header">阅读排行榜</div>
            <div
              class="top"
              v-for="(listItem, index) in hotArticleData"
              :key="listItem.id"
            >
              <p class="num">
                {{ index + 1 > 9 ? index + 1 : "0" + (index + 1) }}
              </p>
              <p class="listTitle">
                {{ listItem.title }}
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 尾部 -->
    <Footer></Footer>
  </div>
</template>

<script>
import { get } from "../utils/request";
import Header from "./components/Header.vue";
import Footer from "./components/Footer";
export default {
  components: {
    Header,
    Footer,
  },
  data() {
    return {
      category: {},
      param: {
        page: 1,
        pageSize: 4,
      },
      articleData: {},
      hotArticleData: {},
    };
  },
  methods: {
    getArticleData() {
      this.param.categoryId = this.category.id;
      // console.log(param);
      get("/index/article/pageQuery", this.param).then((res) => {
        // console.log(res);
        this.articleData = res.data;
      });
    },
    getHotArticleData() {
      get('/index/article/findHotArticles', {categoryId:this.category.id}).then((res) => {
        // console.log(res);
        this.hotArticleData = res.data;
      })
    },
    handleCurrentChange(val) {
      // 使用当前页数与我们分页控制的参数进行赋值
      this.param.page = val;
      this.getArticleData();
      this.getHotArticleData();
    },
    toArticle(item) {
      this.$router.push({
        path: "/article",
        query: item,
      });
    },
  },
  created() {
    this.category = this.$route.query;
    this.getArticleData();
    this.getHotArticleData();
  },
  watch: {
    "$route.query": {
      handler(query) {
        this.category = query;
        // 每次我们点击菜单栏的时候 将page重置为第一页
        this.param.page = 1;
        this.getArticleData();
        this.getHotArticleData();
      },
    },
    deep: true,
  },
};
</script>

<style lang="scss">
.content {
  // background-color: #999;
  min-height: 400px;
  .wrapper {
    .Title {
      background-color: yellowgreen;
      height: 70px;
      color: white;
      font-size: 20px;
      text-align: center;
      line-height: 70px;
    }
    .container {
      display: flex;
      min-height: 400px;
      .arcticleArea {
        flex: 2;
        background-color: rgb(228, 224, 224);
        .arcticle {
          display: flex;
          padding: 7px;
          background-color: rgb(228, 224, 224);
          .logo {
            img {
              width: 150px;
            }
          }
          .right {
            margin-left: 30px;
            line-height: 2.5em;
            .minTitle {
              font-size: 18px;
              cursor: pointer;
            }
          }
        }
      }
      .noData {
        flex: 2;
        background-color: white;
        font-size: 25px;
        text-align: center;
        line-height: 400px;
      }
      .list {
        flex: 1;
        background-color: rgb(228, 224, 224);
        border-left: 1px solid white;
      }
    }
    .list {
      width: 400px;
      .header {
        margin-top: 30px;
        font-size: 20px;
        text-align: center;
        font-weight: bold;
        color: chocolate;
      }
      .top {
        display: flex;
        margin-top: 10px;
        margin-left: 50px;
        font-size: 18px;
        line-height: 1em;
        .num {
          font-weight: bold;
          color: tomato;
        }
        .listTitle {
          margin-left: 10px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }
}
</style>